import { getGroupInfo } from "@/api/chat"
// #ifdef APP-PLUS
import { downToPath, pathByUrl } from "@/services/mediaService"
// #endif


export class Group {
	
	id
	
	name
	
	avatar
	
	saveAvatar
	
	brief
	
	isDelete = false 
	
	isSilent = false
	
	isAddFriend = true
	
	isInvite = true
	
	isInviteAuth = false
	
	isShowRead = false
	
	isShowRedMine = false
	
	isSendCard = false
	
	isSendFile = false
	
	isSendRedPacket = false
	
	
	isRecallMsg = true
	
	userCount = 0
	
	userId
	
	userName
	
	msgSaveTime = -1
	
	async fetchOrigin(){
		const info = await getGroupInfo(this.id)
		const ins = Group.origin2Ins(info)
		this.mixIns(ins)
		// #ifdef APP-PLUS
		pathByUrl(this.avatar).then((path)=>{
			if(path){
				this.saveAvatar = path
			}else{
				downToPath(this.avatar).then((newPath)=>{
					this.saveAvatar = newPath
				})
			}
		})
		// #endif
		return info
	}
	
	mixIns(ins){
		
		const {id,...props} = ins
		Object.assign(this,props)
		return this
	}
	
	
	
	static creatNullIns({id,name,avatar}){
		if(!id){
			throw new Error('id不能为空')
		}
		const ins = new Group()
		ins.id = id
		ins.avatar = avatar
		ins.name = name
		return ins
	}
	
	static origin2Ins(info){
		const ins = new Group()
		ins.id = info.id
		ins.name = info.name
		ins.avatar = info.avatar
		
		ins.brief = info.brief
		ins.isAddFriend = info.isAddFriend == '1'
		ins.isDelete = info.isDelete == '1'
		ins.isInvite = info.isInvite =='1'
		ins.isInviteAuth = info.isInviteAuth == '1'
		ins.isSilent = info.isSilent == '1'
		ins.isShowRead = info.isShowRead == '1'
		ins.isShowRedMine = info.isShowRedMine == '1'
		ins.isSendCard = info.isSendCard == '1'
		ins.isSendFile = info.isSendFile == '1'
		ins.isSendRedPacket = info.isSendRedPacket == '1'
		ins.isRecallMsg = info.isRecallMsg=='1'
		ins.msgSaveTime = info.msgSaveTime
		ins.userCount = info.userCount
		ins.userId = info.userId
		ins.userName = info.userName||''
		return ins
	}
}